package com.qaplus.listener;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.qaplus.constant.Common;
import com.qaplus.entity.QaUser;

/**
 * ServletContextListener application 监听 HttpSessionListener session创建销毁 监听
 * HttpSessionAttributeListener session属性添加、移除、替换 监听
 * 
 * @author wb-zc189961
 *
 */
public class OnlineListener implements ServletContextListener,HttpSessionListener, HttpSessionAttributeListener {
	// 声明一个ServletContext对象
	private ServletContext application = null;

	/**
	 * 给session赋值时触发
	 */
	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {
		System.out.println("给session赋值时触发");
		// 如果登陆成功，则将用户名保存在列表之中   
        @SuppressWarnings("unchecked")
		List<QaUser> allOnlineUser = (List<QaUser>) this.application.getAttribute("allOnlineUser");   
        allOnlineUser.add((QaUser) se.getValue());   
        this.application.setAttribute("allOnlineUser",allOnlineUser);   
        sysOnlineUser();
	}

	/**
	 * 移除session值时触发
	 */
	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {
		System.out.println("移除session值时触发");
	}

	/**
	 * 替换session值时触发
	 */
	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
		System.out.println("替换session值时触发");
	}

	/**
	 * session创建时触发
	 */
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("session创建时触发");
	}

	/**
	 * session销毁时被触发
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("session销毁时被触发");
		// 将用户名称从列表中删除   
        @SuppressWarnings("unchecked")
		List<QaUser> allOnlineUser = (List<QaUser>) this.application.getAttribute("allOnlineUser");   
        QaUser user = (QaUser) se.getSession().getAttribute(Common.CURRENT_OPERATOR.getValue());   
        allOnlineUser.remove(user);   
        this.application.setAttribute("allOnlineUser", allOnlineUser);
        sysOnlineUser();
	}

	/**
	 * 服务器启动
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("服务器启动....");
		// 容器初始化时，向application中存放一个空的容器
		this.application = sce.getServletContext();
		this.application.setAttribute("allOnlineUser", new ArrayList<QaUser>());
		sysOnlineUser();
		
	}

	/**
	 * 服务器销毁
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("服务器销毁....");
	}
	
	public void sysOnlineUser(){
		System.out.println("当前登陆人数：");
		@SuppressWarnings("unchecked")
		List<QaUser> users=(List<QaUser>) this.application.getAttribute("allOnlineUser");
		System.out.println(users.size());
	}

}
